from django.db import models
from django.contrib.auth.models import User
from datetime import date
from django.conf import settings

class Author(models.Model):
    first_name = models.CharField(max_length=100, verbose_name='名')
    last_name = models.CharField(max_length=100, verbose_name='姓')
    date_of_birth = models.DateField(null=True, blank=True, verbose_name='出生日期')
    date_of_death = models.DateField('去世日期', null=True, blank=True)
    avatar = models.ImageField(upload_to='author_avatars/', null=True, blank=True, verbose_name='头像')
    intro = models.TextField(max_length=1000, null=True, blank=True, verbose_name='简介')

    def __str__(self):
        return f'{self.last_name} {self.first_name}'

class Book(models.Model):
    title = models.CharField(max_length=200, verbose_name='书名')
    author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True, verbose_name='作者')
    summary = models.TextField(max_length=1000, help_text='请输入本书简介', verbose_name='简介')
    isbn = models.CharField('ISBN', max_length=13, unique=True)
    cover = models.ImageField(upload_to='book_covers/', null=True, blank=True, verbose_name='封面')
    category = models.CharField(max_length=100, null=True, blank=True, verbose_name='分类')
    tags = models.CharField(max_length=200, null=True, blank=True, verbose_name='标签')
    buy_link = models.URLField(null=True, blank=True, verbose_name='电商购买链接')
    rating = models.FloatField(default=0, verbose_name='评分')
    rating_count = models.IntegerField(default=0, verbose_name='评分人数')
    like_count = models.IntegerField(default=0, verbose_name='点赞数')
    favorite_count = models.IntegerField(default=0, verbose_name='收藏数')

    def __str__(self):
        return self.title

class BookInstance(models.Model):
    id = models.AutoField(primary_key=True)
    book = models.ForeignKey('Book', on_delete=models.RESTRICT, null=True, verbose_name='书籍')
    imprint = models.CharField(max_length=200, verbose_name='版本信息')
    due_back = models.DateField(null=True, blank=True, verbose_name='到期日')
    borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='借阅者')

    LOAN_STATUS = (
        ('m', '维护中'),
        ('o', '已借出'),
        ('a', '可用'),
        ('r', '已预订'),
    )
    status = models.CharField(
        max_length=1,
        choices=LOAN_STATUS,
        blank=True,
        default='m',
        help_text='图书状态',
        verbose_name='状态',
    )

    class Meta:
        ordering = ['due_back']
        verbose_name = '图书实例'
        verbose_name_plural = '图书实例'

    def __str__(self):
        return f'{self.id} ({self.book.title})'

    @property
    def is_overdue(self):
        if self.due_back and date.today() > self.due_back:
            return True
        return False

class BookRating(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')
    book = models.ForeignKey('Book', on_delete=models.CASCADE, verbose_name='书籍')
    score = models.PositiveSmallIntegerField(verbose_name='评分', choices=[(i, f'{i}星') for i in range(1, 6)])
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'book')
        verbose_name = '书籍评分'
        verbose_name_plural = '书籍评分'

class BookReview(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')
    book = models.ForeignKey('Book', on_delete=models.CASCADE, verbose_name='书籍')
    content = models.TextField(verbose_name='评论内容')
    created_at = models.DateTimeField(auto_now_add=True)
    like_count = models.PositiveIntegerField(default=0, verbose_name='点赞数')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='replies', verbose_name='父评论')

    class Meta:
        ordering = ['-created_at']
        verbose_name = '书籍评论'
        verbose_name_plural = '书籍评论'

class BookFavorite(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')
    book = models.ForeignKey('Book', on_delete=models.CASCADE, verbose_name='书籍')
    created_at = models.DateTimeField(auto_now_add=True)
    class Meta:
        unique_together = ('user', 'book')
        verbose_name = '书籍收藏'
        verbose_name_plural = '书籍收藏'
